package com.example.partiu;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListaActivity extends Activity {
	BancoDeDados dbHelper = null;
	 SQLiteDatabase db = null;
	 ListView listViewDisciplinas;
	 
	 ArrayAdapter<String> disciplinasAdapter = null;
	 
	 //Metodo para preencher a List View
	 protected void atualizarLista(){
		 db = dbHelper.getReadableDatabase();
		 Cursor cursor  = db.rawQuery("SELECT * FROM MATERIA", null);
		 cursor.moveToFirst();
		 while(!cursor.isAfterLast()){
			 String nome = cursor.getString(1);
			 String fpermitidas = cursor.getString(4);
			 disciplinasAdapter.add(nome);
			 cursor.moveToNext();
			 
		 }
		 cursor.close();
		 db.close();
		 dbHelper.close();
		 
		 }
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista);
		//Cria um Array Adapter para preencher a list View
				ArrayList<String> disciplinas = new ArrayList<String>();
				disciplinasAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, disciplinas);
				listViewDisciplinas = (ListView)findViewById(R.id.listView1);
				listViewDisciplinas.setAdapter(disciplinasAdapter);
				try{
					dbHelper = new BancoDeDados(getApplicationContext());
					//Chama o metodo para preencher a List View
					atualizarLista();
				}catch(Exception ex){
					new AlertDialog.Builder(this).setMessage(ex.getMessage()).setPositiveButton("OK", null).show();
				}
				//Quando o usuario clica em um item da List View chama esse metodo, que pega o nome da materia 
				//e atribui para uma variavel que atraves do intent chama outra Activity
				listViewDisciplinas.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> disciplinasAdapter, View v, int position, long id){
						try{
						String teste = (String) ((TextView)v).getText().toString();
						Intent intent = new Intent(ListaActivity.this, MateriaActivity.class);
						intent.putExtra("posicao", teste);
						startActivity(intent);
						}catch(Exception ex){
							
						}
					}
				});
			}
			//Chama a Activity para cadastrar a materia
			public void cadastrar (View view){
				
				Intent intent = new Intent(this, CadastrarMateriaActivity.class);
				startActivity(intent);
				
		}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lista, menu);
		return true;
	}

}
